package cn.kais.drawer.bean;

import cn.kais.drawer.UnitUtil;
import com.alibaba.fastjson.JSONObject;

import java.awt.*;

public class BeanLine extends BeanBasic {

    public int top;
    public int left;
    public int width;
    public int height;

    public Color bColor; // 背景颜色

    public int lineWidth = 2; // 线宽
    public Color lineColor; // 线颜色

    public int direct = 1; // 方向：1：水平 2：垂直 3：左下右上 4：左上右下 5：双对角线

    public Graphics2D sd; // 绘制

    public BeanLine(Graphics2D sd, JSONObject com) {
        this.sd = sd;
        this.top = com.getInteger("top");
        this.left = com.getInteger("left");
        this.width = com.getInteger("width");
        this.height = com.getInteger("height");

        this.bColor = UnitUtil.toColor(com.getString("bgColor"));

        this.lineWidth = com.getInteger("lineWidth");
        this.lineColor = UnitUtil.toColor(com.getString("fontColor"));

        this.direct = com.getInteger("lineType");

        this.create();
    }

    private void create() {
        BasicStroke stroke = new BasicStroke(this.lineWidth);

        sd.setColor(this.bColor);
        sd.fillRect(this.left, this.top, this.width, this.height);

        sd.setColor(this.lineColor);
        sd.setStroke(stroke);

        switch (this.direct) {
            case 1: // 水平
                sd.drawLine(this.left, this.top + (this.height / 2), this.left + this.width, this.top + (this.height / 2));
                break;
            case 2: // 垂直
                sd.drawLine(this.left + (this.width / 2), this.top, this.left + (this.width / 2), this.top + this.height);
                break;
            case 3: // 左下右上
                sd.drawLine(this.left, this.top + this.height, this.left + this.width, this.top);
                break;
            case 4: // 左上右下
                sd.drawLine(this.left, this.top, this.left + this.width, this.top + this.height);
                break;
            case 5: // 水平
                sd.drawLine(this.left, this.top, this.left + this.width, this.top + this.height);
                sd.drawLine(this.left, this.top + this.height, this.left + this.width, this.top);
                break;
        }

    }

}